﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using backoffice.Data;
using System.Linq.Expressions;

namespace backoffice.Business
{
    public class BusinessBuy
    {
        #region Create
        public static void createBuy(Buy b)
        {
            try
            {
                dataBuy.createBuy(b);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating buy");
            }
        }

        public static void createBuy(User u, Region r, Plant p, float offer, String currency)
        {
            try
            {
                Buy b = new Buy();
                b.UserID = u.UserID;
                b.RegionID = r.RegionID;
                b.Offer = offer;
                b.Currency = currency;

                dataBuy.createBuy(b);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating buy");
            }
        }
        #endregion

        #region Get
        public static Buy getBuy(Guid guid)
        {
            try
            {
                return dataBuy.getBuy(guid);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting buy");
            }
        }

        public static List<Buy> getBuyByUser(User u)
        {
            try
            {
                Expression<Func<Buy, bool>> whereClause = (Buy b) => (b.UserID == u.UserID);

                return dataBuy.getBuys(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting buy by user");
            }
        }

        public static List<Buy> getBuyByRegion(Region r)
        {
            try
            {
                Expression<Func<Buy, bool>> whereClause = (Buy b) => (b.RegionID == r.RegionID);

                return dataBuy.getBuys(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting buy by region");
            }
        }

        public static List<Buy> getBuyByPlant(Plant p)
        {
            try
            {
                Expression<Func<Buy, bool>> whereClause = (Buy b) => (b.PlantID == p.PlantID);

                return dataBuy.getBuys(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting buy by plant");
            }
        }

        public static List<Buy> getBuyByPlantAndRegion(Plant p, Region r)
        {
            try
            {
                Expression<Func<Buy, bool>> whereClause = (Buy b) => (b.PlantID == p.PlantID) && (b.RegionID == r.RegionID);

                return dataBuy.getBuys(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting buy by plant and region");
            }
        }
        #endregion

        #region Set
        public static Boolean changeBuy(Buy b)
        {
            try
            {
                return dataBuy.changeBuy(b);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error changing buy");
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteSell(Sell s)
        {
            try
            {
                return dataSell.deleteSell(s);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error deleting buy");
            }
        }
        #endregion
    }
}
